import java.util.*;

public class GameBoard {
	
	List<Integer> boardList = new ArrayList<Integer>();
	int [][] boardArray = new int [3][3];
	
	public boolean done(GameBoard board) {
		for(int i = 0; i < 3; i++) {
			for(int k = 0; k < 3; k++) {
				if(boardArray[i][k] == 0) return false;
			}
		}
		return true;
	}
	
	public int win(GameBoard board) {
		int [] lineCounts = new int[8];
		int whoWon = 0;
		boolean win = false;
		
		lineCounts[0] = board.getPiece(0,0) + board.getPiece(0,1) + board.getPiece(0,2);
		lineCounts[1] = board.getPiece(1,0) + board.getPiece(1,1) + board.getPiece(1,2);
		lineCounts[2] = board.getPiece(2,0) + board.getPiece(2,1) + board.getPiece(2,2);
		lineCounts[3] = board.getPiece(0,0) + board.getPiece(1,0) + board.getPiece(2,0);
		lineCounts[4] = board.getPiece(0,1) + board.getPiece(1,1) + board.getPiece(2,1);
		lineCounts[5] = board.getPiece(0,2) + board.getPiece(1,2) + board.getPiece(2,2);
		lineCounts[6] = board.getPiece(0,0) + board.getPiece(1,1) + board.getPiece(2,2);
		lineCounts[7] = board.getPiece(0,2) + board.getPiece(1,1) + board.getPiece(2,0);
		
		while(!win) {
			for(int i = 0; i < 8; i++) {
				if(lineCounts[i] == 3) { win = true; whoWon = 1; break;}
				else if(lineCounts[i] == -3) { win = true; whoWon = -1; break;}
			}
			if(!win) {
				break;
			}
		}
		return whoWon;
	}
	
	public void mimiicList(List<Integer> listHanded) {
		for(int i = 0; i < 8; i++) {
			boardList.set(i, listHanded.get(i));
			for(int k = 0; i < 3; k++) {
				for(int j = 0; j < 3; j++) {
					boardArray[k][j] = boardList.get(i);
				}
			}
		}

	}
	
	public GameBoard returnBoard() {
		GameBoard board = new GameBoard();
		board.boardArray = boardArray;
		board.boardList = boardList;
		return board;
	}

	public void createNew() {
		for(int i = 0; i < 9; i++) {
			boardList.add(0);
		}
		for(int j = 0; j < 3; j++) {
			for(int k = 0; k < 3; k++) {
				boardArray[j][k] = 0;
			}
		}

	}
	
	public int getPiece(int row, int column) {
		return boardArray[row][column];
	}
	
	public void makeMove(int row, int column, GameBoard board) {
		if(board.getPiece(row, column) == 0) {
			boardArray[row][column] = -1;
			if(row == 0 && column == 0) boardList.set(0, -1);
			else if(row == 0 && column == 1) boardList.set(1, -1);
			else if(row == 0 && column == 2) boardList.set(2, -1);
			else if(row == 1 && column == 0) boardList.set(3, -1);
			else if(row == 1 && column == 1) boardList.set(4, -1);
			else if(row == 1 && column == 2) boardList.set(5, -1);
			else if(row == 2 && column == 0) boardList.set(6, -1);
			else if(row == 2 && column == 1) boardList.set(7, -1);
			else if(row == 2 && column == 2) boardList.set(8, -1);
		}
	}
	
	public void makeOpponentMove(int row, int column, GameBoard board) {
		boardArray[row][column] = 1;
		
		if(row == 0 && column == 0) boardList.set(0, 1);
		else if(row == 0 && column == 1) boardList.set(1, 1);
		else if(row == 0 && column == 2) boardList.set(2, 1);
		else if(row == 1 && column == 0) boardList.set(3, 1);
		else if(row == 1 && column == 1) boardList.set(4, 1);
		else if(row == 1 && column == 2) boardList.set(5, 1);
		else if(row == 2 && column == 0) boardList.set(6, 1);
		else if(row == 2 && column == 1) boardList.set(7, 1);
		else if(row == 2 && column == 2) boardList.set(8, 1);
	}
	
	/**
	 * Takes in a type board, and prints out the string representation of that board
	 */
	public void toString(GameBoard board) {
		String [][] tempArray = new String[3][3];
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				if(board.getPiece(i,j) == -1) tempArray[i][j] = "X";
				else if(board.getPiece(i,j) == 0) tempArray[i][j] = " ";
				else if(board.getPiece(i,j) == 1) tempArray[i][j] = "O";
			}
		}
		System.out.format("%s | %s | %s", tempArray[0][0], tempArray[0][1], tempArray[0][2]);
		System.out.println();
		System.out.println("=========");
		System.out.format("%s | %s | %s", tempArray[1][0], tempArray[1][1], tempArray[1][2]);
		System.out.println();
		System.out.println("=========");
		System.out.format("%s | %s | %s", tempArray[2][0], tempArray[2][1], tempArray[2][2]);
		System.out.println();
	}

	public List<Integer> toList(GameBoard board) {
		if(boardList.isEmpty()) {
			board.createNew();
		}
		return board.boardList;
	}
	
	public int [][] toArray(GameBoard board) {
		for(int i = 0; i < 3; i++) {
			for(int j = 0; i < 3; i++) {
				boardArray[i][j] = board.getPiece(i,j);
			}
		}
		return boardArray;
	}
}
